package com.wwz;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriteTest {
    String PATH = "/Users/sailor/Desktop/workSpace/wwz-poi";

    @Test
    public void testWrite03() throws IOException {
        // 创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        // 创建一个工作表
        Sheet sheet = workbook.createSheet("美好的一天");
        //创建一个行
        Row row = sheet.createRow(0);
        //创建一个单元格
        Cell cell = row.createCell(0);
        cell.setCellValue("遇见小蜗");
        Cell cell1 = row.createCell(1);
        cell1.setCellValue("7次");

        //第二行
        Row row1 = sheet.createRow(1);
        Cell cell2 = row1.createCell(0);
        cell2.setCellValue("美好的第二天时间");
        Cell cell22 = row1.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        //生成一张表
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + time + "03.xls");

        workbook.write(fileOutputStream);

        fileOutputStream.close();

        System.out.println("生成完毕");

    }

    @Test
    public void testWrite07() throws IOException {
        // 创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建一个工作表
        Sheet sheet = workbook.createSheet("美好的一天07");
        //创建一个行
        Row row = sheet.createRow(0);
        //创建一个单元格
        Cell cell = row.createCell(0);
        cell.setCellValue("遇见小蜗");
        Cell cell1 = row.createCell(1);
        cell1.setCellValue("7次");

        //第二行
        Row row1 = sheet.createRow(1);
        Cell cell2 = row1.createCell(0);
        cell2.setCellValue("美好的第二天时间");
        Cell cell22 = row1.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        //生成一张表
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + time + "07.xlsx");

        workbook.write(fileOutputStream);

        fileOutputStream.close();

        System.out.println("生成完毕");

    }

    @Test
    public void testWrite03BigData() throws IOException {
        long start = System.currentTimeMillis();
        //创建一个薄
        Workbook workbook = new HSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //xls 最大支持65536行
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("finished");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "bigdata03.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println((double) (end - start) / 1000);
    }

    @Test
    public void testWrite07BigData() throws IOException {
        long start = System.currentTimeMillis();
        //创建一个薄
        Workbook workbook = new XSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //xls 最大支持65536行
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("finished");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "bigdata07.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println((double) (end - start) / 1000);
    }
}
